var express = require('express');
var adminLoginRouter = express.Router();
const SQLQuery = require('../utils/query.js');
let jwt = require('../utils/JWT.js');

adminLoginRouter.post('/login', async function (req, res) {
    try {
        let queryObj = req.body;
        var sql = 'select * from admin_account WHERE name = ? AND password = ?';
        let result = await SQLQuery.query(sql, [queryObj.name, queryObj.password]);
        if (result.length > 0) {
            const token = jwt.generate({
                username: result[0].name
            }, "1h")
            res.header("Authorization", token)//将token设置到响应头
            res.send(global.common.resJson(0, 'success!', null))
        } else {
            res.send(global.common.resJson(1, '用户名密码错误', null))
        }
    } catch (err) {
        res.send(err);
    }

})

module.exports = adminLoginRouter;